package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TbBrandService;
import com.dongdongshop.service.TbSpecificationService;
import com.dongdongshop.service.TbTypeTemplateService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("tbTypeTemplateController")
public class TbTypeTemplateController {
    @Reference
    private TbTypeTemplateService tbTypeTemplateService;
    @Reference
    private TbBrandService tbBrandService;
    @Reference
    private TbSpecificationService tbSpecification;
    @RequestMapping("template")
    public String toTemplate(Model model) {
        List<TbBrand> brandList = tbBrandService.findAll();
        List<TbSpecification> specifications = tbSpecification.selectAll();
        model.addAttribute("brandList",brandList);
        model.addAttribute("specList",specifications);
        return "admin/type_template";
    }
    @RequestMapping("findTemp")
    @ResponseBody
    public List<TbTypeTemplate> getInfo(){
        List<TbTypeTemplate> tbTypeTemplates = tbTypeTemplateService.selectAll();
        return tbTypeTemplates;
    }
    @RequestMapping("toadd")
    public String toAdd(Model model){
//        List<TbBrand> brandList = tbBrandService.findAll();
//        model.addAttribute("brandList",brandList);
        return "admin/type/add";
    }
    @RequestMapping("toAddTemp")
    @ResponseBody
    public Result toAdd(TbTypeTemplate tbTypeTemplate){
            tbTypeTemplateService.addAll(tbTypeTemplate);
            return  new Result(10000,"增加成功",null);
    }
    //回显
    @RequestMapping("echoTemp/{id}")
    @ResponseBody
    public Result toEcho(@PathVariable("id")Long id){
        if (id==null){
            return Result.build(ConstantEnum.ARCS_NULL);
        }
        TbTypeTemplate type = tbTypeTemplateService.updateType(id);
        return Result.build(ConstantEnum.SUCCESS).setRows(type);
    }
    //修改
    @RequestMapping("updateTypeInfo")
    @ResponseBody
    public Result updateTypeInfo(TbTypeTemplate tbTypeTemplate){
        if (tbTypeTemplate==null){
            return Result.build(ConstantEnum.ARCS_NULL);
        }
        tbTypeTemplateService.updateTypeInfo(tbTypeTemplate);
        return Result.build(ConstantEnum.SUCCESS);
    }
    @RequestMapping("deleteTypeInfo")
    @ResponseBody
    public Result toDelete(Long id){
        tbTypeTemplateService.deleteById(id);
        return  Result.build(ConstantEnum.SUCCESS);
    }
}
